﻿using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.report;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.抖音出入库;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.钉钉;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.UC.面料库存.入库;
using DXApplicationTangche.UC.项目成果管理.service;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form断料裁剪预览 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private List<String> kashiyamaFabric = new List<string>();

        private MaterialPlanConfirmIndexDTO materialPlanConfirmIndexDTO = new MaterialPlanConfirmIndexDTO();
        private List<DouyinRequestDTO> douyinRequestDTOs = new List<DouyinRequestDTO>();
        private List<StockInOutDetailDTO> StockInOutDetailDTOs = new List<StockInOutDetailDTO>();

        public Form断料裁剪预览(MaterialPlanConfirmIndexDTO materialPlanConfirmIndexDTO)
        {
            InitializeComponent();
            this.materialPlanConfirmIndexDTO = materialPlanConfirmIndexDTO;
            this.gridControl面料库存固定损耗.DataSource = this.materialPlanConfirmIndexDTO.materialRequirementDTOS;
            this.gridControl订单明细.DataSource = this.materialPlanConfirmIndexDTO.wipBatchWIthMBomDTOS;
            //根据id生成成衣出库计划
            this.douyinRequestDTOs = 面料审核Service.getGarmentStockOutPlanById(materialPlanConfirmIndexDTO.id);
            this.gridControl扫码明细.DataSource = this.douyinRequestDTOs;
            if (this.materialPlanConfirmIndexDTO.tailorComplete)
            {
                this.textBox扫描工牌.Enabled = false;
                this.label提示.Text = "裁剪已完成，请勿重复提交";
            }
            #region
            this.kashiyamaFabric.Add("11117_325");
            this.kashiyamaFabric.Add("11120 C83");
            this.kashiyamaFabric.Add("11120_C83");
            this.kashiyamaFabric.Add("11124_892");
            this.kashiyamaFabric.Add("11138_816");
            this.kashiyamaFabric.Add("11154_537");
            this.kashiyamaFabric.Add("11158_373");
            this.kashiyamaFabric.Add("1405472-1");
            this.kashiyamaFabric.Add("1405826-5");
            this.kashiyamaFabric.Add("1411115-1");
            this.kashiyamaFabric.Add("1411115-2");
            this.kashiyamaFabric.Add("1411627-1");
            this.kashiyamaFabric.Add("1414423-5");
            this.kashiyamaFabric.Add("1415541-1");
            this.kashiyamaFabric.Add("1415541-4");
            this.kashiyamaFabric.Add("1415578-3-D");
            this.kashiyamaFabric.Add("1436697-1");
            this.kashiyamaFabric.Add("1436697-2");
            this.kashiyamaFabric.Add("21143 H09");
            this.kashiyamaFabric.Add("21143_H09");
            this.kashiyamaFabric.Add("21156_495");
            this.kashiyamaFabric.Add("21156_625");
            this.kashiyamaFabric.Add("21170_315");
            this.kashiyamaFabric.Add("21170_323");
            this.kashiyamaFabric.Add("21170_375");
            this.kashiyamaFabric.Add("21170_455");
            this.kashiyamaFabric.Add("21170_484");
            this.kashiyamaFabric.Add("41119_892");
            this.kashiyamaFabric.Add("41121_326");
            this.kashiyamaFabric.Add("41142_113");
            this.kashiyamaFabric.Add("41171_325");
            this.kashiyamaFabric.Add("41171_U01");
            this.kashiyamaFabric.Add("41173_605");
            this.kashiyamaFabric.Add("41173_625");
            this.kashiyamaFabric.Add("41173_U53");
            this.kashiyamaFabric.Add("CAN-001");
            this.kashiyamaFabric.Add("CAN-002");
            this.kashiyamaFabric.Add("CAN-003");
            this.kashiyamaFabric.Add("CAN-004");
            this.kashiyamaFabric.Add("CAN-005");
            this.kashiyamaFabric.Add("CAN-006");
            this.kashiyamaFabric.Add("CAN-007");
            this.kashiyamaFabric.Add("CAN-008");
            this.kashiyamaFabric.Add("CAN-009");
            this.kashiyamaFabric.Add("JM-04");
            this.kashiyamaFabric.Add("JM-06");
            this.kashiyamaFabric.Add("JM-09-1");
            this.kashiyamaFabric.Add("JM-09-2");
            this.kashiyamaFabric.Add("JM-10-1");
            this.kashiyamaFabric.Add("JM-10-2");
            this.kashiyamaFabric.Add("JM-10-3");
            this.kashiyamaFabric.Add("JM-10-4");
            this.kashiyamaFabric.Add("JM-10-5");
            this.kashiyamaFabric.Add("JM-12-1");
            this.kashiyamaFabric.Add("JM-12-2");
            this.kashiyamaFabric.Add("JM-12-3");
            this.kashiyamaFabric.Add("JM-15-2");
            this.kashiyamaFabric.Add("JM-19-4");
            this.kashiyamaFabric.Add("JM-19-5");
            this.kashiyamaFabric.Add("JM-21");
            this.kashiyamaFabric.Add("JM-26");
            this.kashiyamaFabric.Add("JM-26-1");
            this.kashiyamaFabric.Add("JM-27");
            this.kashiyamaFabric.Add("JM-27-1");
            this.kashiyamaFabric.Add("JM-28-1");
            this.kashiyamaFabric.Add("JM-28-2");
            this.kashiyamaFabric.Add("JM-29-1");
            this.kashiyamaFabric.Add("JM-29-2");
            this.kashiyamaFabric.Add("JM-29-3");
            this.kashiyamaFabric.Add("JM-30-1");
            this.kashiyamaFabric.Add("JM-30-2");
            this.kashiyamaFabric.Add("JM-32-1");
            this.kashiyamaFabric.Add("JM-32-2");
            this.kashiyamaFabric.Add("JM-33-1");
            this.kashiyamaFabric.Add("JM-33-2");
            this.kashiyamaFabric.Add("JM-33-3");
            this.kashiyamaFabric.Add("JM-33-4");
            this.kashiyamaFabric.Add("JM-35-1");
            this.kashiyamaFabric.Add("JM-35-2");
            this.kashiyamaFabric.Add("JM-40");
            this.kashiyamaFabric.Add("JM-41");
            this.kashiyamaFabric.Add("JM-42");
            this.kashiyamaFabric.Add("JM42BK");
            this.kashiyamaFabric.Add("JM-44");
            this.kashiyamaFabric.Add("JM-45");
            this.kashiyamaFabric.Add("JM-46");
            this.kashiyamaFabric.Add("JM-47");
            this.kashiyamaFabric.Add("JMC24-1");
            this.kashiyamaFabric.Add("JMOX1");
            this.kashiyamaFabric.Add("JMOX2");
            this.kashiyamaFabric.Add("JMOX3");
            this.kashiyamaFabric.Add("JMOX5");
            this.kashiyamaFabric.Add("JMOX6");
            this.kashiyamaFabric.Add("SS090");
            this.kashiyamaFabric.Add("SS105");
            this.kashiyamaFabric.Add("SS107");
            this.kashiyamaFabric.Add("SS108");
            this.kashiyamaFabric.Add("SS138");
            #endregion
        }

        private void textBox扫描工牌_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫描工牌.Text))
                {
                    MessageBox.Show("无扫描信息");
                    return;
                }
                //判断扫描的工牌是否正确
                String mnemonic = 项目成果Service.get判断扫描员工码是否正确(this.textBox扫描工牌.Text.Trim());
                if (String.IsNullOrWhiteSpace(mnemonic))
                {
                    this.textBox扫描工牌.Clear();
                    MessageBox.Show("扫描员工码错误，请重新扫描");
                    return;
                }
                面料审核Service.tailorFinish(this.textBox扫描工牌.Text.Trim(), this.materialPlanConfirmIndexDTO);
                MessageBox.Show("操作完成");
            }
        }

        private void gridView订单明细_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
        }

        private void gridView面料库存固定损耗_DoubleClick(object sender, EventArgs e)
        {
            //if (this.materialPlanConfirmIndexDTO.tailorComplete)
            //{
            //    MessageBox.Show("裁剪已完成，无法修改实际出库数量");
            //    return;
            //}
            String fname = this.gridView面料库存固定损耗.GetRowCellValue(this.gridView面料库存固定损耗.FocusedRowHandle, "fname").ToString();
            new Form修改实际出库数量(materialPlanConfirmIndexDTO.id,fname).ShowDialog();
            //刷新
            this.刷新();
        }
        private void 刷新()
        {
            List<MaterialPlanConfirmIndexDTO> materialPlanConfirmIndexDTOs = 面料审核Service.getMaterialPlanConfirmIndexAll();
            foreach(MaterialPlanConfirmIndexDTO materialPlanConfirmIndexDTO in materialPlanConfirmIndexDTOs)
            {
                if (this.materialPlanConfirmIndexDTO.id.Equals(materialPlanConfirmIndexDTO.id))
                {
                    this.gridControl面料库存固定损耗.DataSource = materialPlanConfirmIndexDTO.materialRequirementDTOS;
                }
            }
        }

        private void simpleButtonPDF_Click(object sender, EventArgs e)
        {
            MaterialRequirementQueryDTO materialRequirementQueryDTO裁剪 = 面料审核Service.getMaterialRequirementQueryById(materialPlanConfirmIndexDTO.id);
            XtraReport面料领用出库单 report = new XtraReport面料领用出库单(materialRequirementQueryDTO裁剪);
            ////  循环取details并清洗尺寸
            ////report.DataSource = this.materialRequirementQueryDTO;
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
            string saveDirectoryPath = @"pdf\";
            //  1.生成pdf文件保存到程序
            report.ExportToPdf(saveDirectoryPath + materialRequirementQueryDTO裁剪.fName + @".pdf");
            // 上传pdf
            面料审核Service.uploadFile(
                            materialRequirementQueryDTO裁剪.fName,
                            saveDirectoryPath + materialRequirementQueryDTO裁剪.fName + @".pdf"
                        );
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(textBox扫码.Text))
                {
                    BlnResult blnResult =
                        面料审核Service.判断款式是否符合(
                            this.douyinRequestDTOs,
                            MTO订单Service.getOrderCommitDtoByBarcode(textBox扫码.Text.Trim()), //  order commit dto 
                            this.textBox扫码.Text.Trim()
                        );
                    OrderCommitDTO返回 orderCommitDTO = MTO订单Service.getOrderCommitDtoByBarcode(textBox扫码.Text.Trim());
                    if (!blnResult.if款式)
                    {
                        MessageBox.Show("扫描成衣款式信息与已选款式不符");
                        return;
                    }
                    if (blnResult.if条码存在)
                    {
                        MessageBox.Show("条码已存在，请勿重复出库");
                        return;
                    }
                    else
                    {
                        //  条码不重复，刷新一览
                        this.gridView扫码明细.RefreshData();
                        try { orderCommitDTO.styleSizeCd = orderCommitDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        this.StockInOutDetailDTOs.Add(new StockInOutDetailDTO(this.textBox扫码.Text.Trim(), orderCommitDTO.styleNameCn, orderCommitDTO.materialCode, orderCommitDTO.styleSizeCd));
                    }
                    this.textBox扫码.Clear();
                }
                else
                {
                    MessageBox.Show("成衣码为空");
                    this.textBox扫码.Clear();
                    return;
                }
            }
        }
        

        private void simpleButton出库_Click(object sender, EventArgs e)
        {
            if (this.douyinRequestDTOs.Count > 0)
            {
                String re = 面料审核Service.saveDouyinRequest(this.materialPlanConfirmIndexDTO.id, this.douyinRequestDTOs);
                订单一览新.订单预览新.service.PlainMessageResult plainMessageResult = FunctionHelper.JsonDeserialization<订单一览新.订单预览新.service.PlainMessageResult>(re);
                //返回值200 保存成功
                if ("200".Equals(plainMessageResult.errorCode))
                {
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show(plainMessageResult.errorMessage);
                }
            }
        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码3"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
        }

        private void barButtonItem打印pdf_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MaterialRequirementQueryDTO materialRequirementQueryDTO裁剪 = 面料审核Service.getMaterialRequirementQueryById(materialPlanConfirmIndexDTO.id);
            XtraReport面料领用出库单 report = new XtraReport面料领用出库单(materialRequirementQueryDTO裁剪);
            ////  循环取details并清洗尺寸
            ////report.DataSource = this.materialRequirementQueryDTO;
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
            string saveDirectoryPath = @"pdf\";
            //  1.生成pdf文件保存到程序
            report.ExportToPdf(saveDirectoryPath + materialRequirementQueryDTO裁剪.fName + @".pdf");
            // 上传pdf
            面料审核Service.uploadFile(
                            materialRequirementQueryDTO裁剪.fName,
                            saveDirectoryPath + materialRequirementQueryDTO裁剪.fName + @".pdf"
                        );
        }

        private void barButtonItem扫码确认_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.douyinRequestDTOs.Count > 0)
            {
                String re = 面料审核Service.saveDouyinRequest(this.materialPlanConfirmIndexDTO.id, this.douyinRequestDTOs);
                订单一览新.订单预览新.service.PlainMessageResult plainMessageResult = FunctionHelper.JsonDeserialization<订单一览新.订单预览新.service.PlainMessageResult>(re);
                //返回值200 保存成功
                //发送钉钉
                if ("200".Equals(plainMessageResult.errorCode))
                {
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show(plainMessageResult.errorMessage);
                }
            }
        }

        private void advBandedGridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
            }
        }

        private void 打印订单纸ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String id = this.materialPlanConfirmIndexDTO.id;
            String batchNo = this.advBandedGridView订单明细.GetRowCellValue(this.advBandedGridView订单明细.FocusedRowHandle, "batchNo").ToString();
            if((String.IsNullOrEmpty(id))||(String.IsNullOrEmpty(batchNo)))
            {
                MessageBox.Show("请选择订单");
                return;
            }
            WIPPlanBatchDTO wIPPlanBatchDTO = 编排生产批Service.getWIPPlanBatchByBatchNo(id, batchNo);
            new Form订单预览新(wIPPlanBatchDTO, "单张打印订单纸").ShowDialog();
        }

        //批量打印订单纸
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            String id = this.materialPlanConfirmIndexDTO.id;
            foreach (WIPBatchWIthMBomDTO wipBatchWIthMBomDTO in this.materialPlanConfirmIndexDTO.wipBatchWIthMBomDTOS)
            {
                String batchNo = wipBatchWIthMBomDTO.batchNo;
                if ((String.IsNullOrEmpty(id)) || (String.IsNullOrEmpty(batchNo)))
                {
                    MessageBox.Show("请选择订单");
                    return;
                }
                WIPPlanBatchDTO wIPPlanBatchDTO = 编排生产批Service.getWIPPlanBatchByBatchNo(id, batchNo);
                new Form订单预览新(wIPPlanBatchDTO);
            }
        }

        private void 编排生产计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String id = this.materialPlanConfirmIndexDTO.id;
            String batchNo = this.advBandedGridView订单明细.GetRowCellValue(this.advBandedGridView订单明细.FocusedRowHandle, "batchNo").ToString();
            if ((String.IsNullOrEmpty(id)) || (String.IsNullOrEmpty(batchNo)))
            {
                MessageBox.Show("请选择订单");
                return;
            }
            WIPPlanBatchDTO wIPPlanBatchDTO = 编排生产批Service.getWIPPlanBatchByBatchNo(id, batchNo);
            new Form订单预览新(wIPPlanBatchDTO,"单张打印订单纸").ShowDialog();
        }

        private void barButtonItem导出excel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<KashiyamaWipDTO> kashiyamaWipDTOs = 面料审核Service.getKashiyamaWipByPlanId(materialPlanConfirmIndexDTO.id);
            foreach (KashiyamaWipDTO kashiyamaWipDTO in kashiyamaWipDTOs)
            {
                kashiyamaWipDTO.styleSizeCd = kashiyamaWipDTO.styleSizeCd.Replace("EGS_GROUP_SIZE-", "");
            }
            DataTable dt = DataUtil.ToDataTable<KashiyamaWipDTO>(kashiyamaWipDTOs);
            new Form坚山面料库存(dt).ShowDialog();
        }

        private void barButtonItem发送钉钉_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraReport抖音出库单 report = new XtraReport抖音出库单(this.douyinRequestDTOs);

            //StockInOutTitleDTO 出库单DTO = new StockInOutTitleDTO(
            //                            "",
            //                            DateTime.Now,
            //                            "0",
            //                            "抖音出库",
            //                            "fengjinyue01",
            //                            "冯金月",
            //                            0,
            //                            "8",
            //                            "退回冻结存货",
            //                            "抖音出库"
            //                        );
            ////出库单时间
            //String stockTime = DateTime.Now.ToString();
            //try { stockTime = stockTime.Replace(" ", "").Replace("/", "").Replace(":", ""); } catch { }
            //String id = "douyinStockOut_" + stockTime;
            ////钉钉
            //try
            //{
            //    //  上传pdf:
            //    string saveDirectoryPath = @"pdf\";
            //    //  1.先本地生成文件
            //    report.ExportToPdf(saveDirectoryPath + id + @".pdf");
            //    //  2.上传本地文件
            //    String result = 成品出库单Service.uploadStockInOutFileByCode(id, saveDirectoryPath + id + @".pdf");
            //    //  3.删除本地文件
            //    //File.Delete(saveDirectoryPath + re);
            //    //  钉钉
            //    钉钉Service.start(new ProcessStartDTO(调拨出库类型Consts.抖音出库, 出库单DTO, result).build("17866554337", 409162359L));
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
            //}
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
        }
    }
}
